package exttable.formgenerator;

import dbutils.DbUtils;
import exttable.dbclasses.Phase;
import exttable.dbclasses.Probationer;
import exttable.dbclasses.Visit;
import exttable.questionnaire.QuestionnaireService;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 02.11.2010
 * Time: 11:35:39
 * To change this template use File | Settings | File Templates.
 */
public class SelectPhaseAndProbationerFormForQuestionnaire {
    private JFrame frame;
    private JPanel panel;
    private List<Phase> phaseList = new ArrayList<Phase>();
    private JComboBox phaseBox;
    private static JComboBox probationerBox;
    private static DefaultComboBoxModel dfcbModelProbationer;
    private JComboBox visitBox;
    private static DefaultComboBoxModel dfcbModelVisit;
    private List<Visit> visitList = new ArrayList<Visit>();
    private static String operation = "";

    private JButton nextButton;

    public static JComboBox getProbationerBox() {
        return probationerBox;
    }

    public static String getOperation() {
        return operation;
    }

    public static void setOperation(String operation) {
        SelectPhaseAndProbationerFormForQuestionnaire.operation = operation;
    }

    public SelectPhaseAndProbationerFormForQuestionnaire(String operation) {
        this.operation = operation;
        phaseList = Phase.getPhaseList();
        frame = new JFrame(DbUtils.ModeOfOperation);
        panel = new JPanel();
        phaseBox = new JComboBox(phaseList.toArray());
        phaseBox.setPreferredSize(new Dimension(300, 20));
        phaseBox.addActionListener(ListenerFactory.reloadProbationerList());
        panel.add(new JLabel(Localisation.getMessage("form.phase")));
        panel.add(phaseBox);
        dfcbModelProbationer = new DefaultComboBoxModel();
        probationerBox = new JComboBox(dfcbModelProbationer);
        probationerBox.addActionListener(ListenerFactory.reloadVisitList());
        panel.add(new JLabel(Localisation.getMessage("menu.probationer")));
        panel.add(probationerBox);
        panel.add(new JLabel(Localisation.getMessage("form.visit")));
        dfcbModelVisit = new DefaultComboBoxModel();
        visitBox = new JComboBox(dfcbModelVisit);
        panel.add(visitBox);
        nextButton = new JButton(Localisation.getMessage("form.continue"));
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Probationer p = (Probationer) probationerBox.getSelectedItem();
                    Visit v = (Visit) visitBox.getSelectedItem();
                    new QuestionnaireService(p, v.getVisitId());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        panel.add(nextButton);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(520, 250);
        frame.setVisible(true);
    }


    public static List<Probationer> getProbationerListByPhase(String phaseId) {
        /*
        This method returns List<Probationer> of specifical phase
         */
        List<Probationer> toReturn = new ArrayList<Probationer>();
        try {
            Connection conn = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = conn.prepareStatement("SELECT " +
                    "  * " +
                    "FROM" +
                    "  probationers" +
                    "  INNER JOIN groups ON (probationers.group_id=groups.group_id)" +
                    "  INNER JOIN dic_phase ON (groups.phase_id = dic_phase.phase_id)" +
                    "WHERE dic_phase.phase_id=? ORDER BY probationer_code");
            ps.setString(1, phaseId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Probationer probationer = new Probationer();
                probationer.setProbationer_id(Integer.parseInt(rs.getString("probationer_id")));
                probationer.setProbationer_code(rs.getString("probationer_code"));
                probationer.setGroup_id(Integer.parseInt(rs.getString("group_id")));
                toReturn.add(probationer);
            }
            conn.close();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return toReturn;
    }

    public static void changeDFCBModel(JComboBox phaseBox) {

        Phase phase = (Phase) phaseBox.getSelectedItem();
        dfcbModelProbationer.removeAllElements();
        List<Probationer> probationersList = SelectPhaseAndProbationerFormForQuestionnaire.getProbationerListByPhase(phase.getPhaseId());
        for (int i = 0; i < probationersList.size(); i++) {
            dfcbModelProbationer.addElement(probationersList.get(i));
        }
    }

    public static void changeVisitDFCBModel(JComboBox probationerBox) {
        if (probationerBox.getSelectedItem() != null) {
            Probationer p = (Probationer) probationerBox.getSelectedItem();
            dfcbModelVisit.removeAllElements();
            List<Visit> visitsList = SelectPhaseAndProbationerFormForQuestionnaire.getVisitList(p.getProbationer_id());
            for (int i = 0; i < visitsList.size(); i++) {
                dfcbModelVisit.addElement(visitsList.get(i));
            }
        }


    }

    public static List<Visit> getVisitList(int probationer_id) {
        List<Visit> toReturn = new ArrayList<Visit>();
        try {
            Connection conn = DbUtils.getDBConnectionGeneral();
            String var="NOT";
            if(operation.equals("edit")){
                var="";
            }
            PreparedStatement ps = conn.prepareStatement("SELECT dic_visits.vizit_id," +
                    "dic_visits.name_ru, " +
                    "dic_visits.name_en, " +
                    "dic_visits.name_kk " +
                    "FROM " +
                    "dic_visits " +
                    "WHERE dic_visits.vizit_id "+
                    var+"  IN( " +
                    "SELECT result_answers.vizit_id FROM result_answers " +
                    "WHERE result_answers.probationer_id=? " +
                    ")" +
                    " AND dic_visits.vizit_id < 182");
            ps.setInt(1, probationer_id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Visit visit = new Visit();
                visit.setVisitId(Integer.parseInt(rs.getString("vizit_id")));
                visit.setNameRu(rs.getString("name_ru"));
                visit.setNameEn(rs.getString("name_en"));
                visit.setNameKz(rs.getString("name_kk"));
                toReturn.add(visit);
            }
            conn.close();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return toReturn;
    }


}


